package logic

import (
	"context"
	"errors"
	"pa/models/mysql"

	"pa/parpc/internal/svc"
	"pa/parpc/parpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodAddLogic {
	return &GoodAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodAddLogic) GoodAdd(in *parpc.GoodAddRequest) (*parpc.GoodAddResponse, error) {
	// todo: add your logic here and delete this line
	var findGood mysql.Good
	mysql.DB.Debug().Where("title=?", in.Title).Find(&findGood)
	if in.Title == findGood.Title {
		return nil, errors.New("商品已存在,禁止重复添加")
	}

	good := mysql.Good{
		Title: in.Title,
		Image: in.Image,
		Price: float64(in.Price),
	}
	mysql.DB.Debug().Create(&good)

	return &parpc.GoodAddResponse{
		GoodId: int64(good.ID),
	}, nil
}
